package com.zmn.oms.admin.controller.order;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.dubbo.dto.zmn.order.*;
import com.zmn.oms.dubbo.dto.zmn.order.shop.NewShopOrderDIO;
import com.zmn.oms.dubbo.dto.zmn.order.shop.OrderAmountByCalcDIO;
import com.zmn.oms.dubbo.dto.zmn.order.shop.OrderAmountByCalcDRO;
import com.zmn.oms.dubbo.dto.zmn.orderstock.OrderStockDIO;
import com.zmn.oms.dubbo.dto.zmn.orderstock.OrderStockDRO;
import com.zmn.oms.dubbo.dto.zmn.serviceitem.OrderServiceItemDIO;
import com.zmn.oms.dubbo.interfaces.zmn.channel.order.ZsChannelOrderRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.normal.order.ZsNormalOrderRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.normal.orderstock.OrderStockRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.shop.order.ShopOrderRemoteService;
import com.zmn.oms.model.bo.order.OrderBO;
import com.zmn.oms.model.dto.order.zmn.ZsBCustomerNewOrderDTO;
import com.zmn.oms.model.dto.order.zmn.ZsBCustomerUpdateOrderDTO;
import com.zmn.oms.model.dto.order.zmn.ZsNormalNewOrderDTO;
import com.zmn.oms.model.dto.product.OrderProductDTO;
import com.zmn.oms.model.dto.work.masterwork.CompleteDTO;
import com.zmn.oms.model.dto.work.modify.must.ZsCancelDTO;
import com.zmn.oms.model.entity.ins.InsOrder;
import com.zmn.oms.model.entity.work.DistributeMasterQuery;
import com.zmn.oms.model.vo.work.ZsNormalUpdateOrderVO;
import com.zmn.oms.services.interfaces.datasync.ElasticSearchDataSyncService;
import com.zmn.oms.services.interfaces.ins.InsOrderService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkBService;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsDistributeWorkBService;
import com.zmn.oms.zmn.normal.business.interfaces.order.ZsNormalOrderBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalWorkBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.masterwork.ZsNormalMasterWorkBService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：
 *
 * @author liuying
 * @date 2018/11/21 15:36
 */
@Controller
@RequestMapping("/order")
public class OrderTestController extends OmsBaseController {

    @Reference(version = com.zmn.oms.dubbo.utils.DubboConsts.INTERFACE_VERSION, check = false)
    protected ZsChannelOrderRemoteService zsChannelOrderRemoteService;
    @Reference(version = com.zmn.oms.dubbo.utils.DubboConsts.INTERFACE_VERSION, check = false)
    protected ZsNormalOrderRemoteService zsNormalOrderRemoteService;
    @Reference(version = com.zmn.oms.dubbo.utils.DubboConsts.INTERFACE_VERSION, check = false)
    protected ShopOrderRemoteService shopOrderRemoteService;
    @Reference(version = com.zmn.oms.dubbo.utils.DubboConsts.INTERFACE_VERSION, check = false)
    protected OrderStockRemoteService orderStockRemoteService;

    @Autowired
    private ZsNormalOrderBService zsNormalOrderBService;
    @Autowired
    private ElasticSearchDataSyncService elasticSearchDataSyncService;
    @Resource
    private ZsDistributeWorkBService zsDistributeWorkBService;
    @Autowired
    private ZsOrderWorkBService zsOrderWorkBService;
    @Autowired
    private ZsNormalWorkBService zsNormalWorkBService;
    @Autowired
    private InsOrderService insOrderService;
    @Autowired
    private ZsNormalMasterWorkBService zsNormalMasterWorkBService;

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/getOrder")
    @ResponseBody
    @SuppressWarnings("all")
    public ResultDTO getTesOrder(Long orderId) {
        try {
            ResponseDTO<OrderDRO> responseDTO = zsNormalOrderRemoteService.getOrderById(orderId);
            return new ResultDTO<>(true, responseDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/getOrder1")
    @ResponseBody
    @SuppressWarnings("all")
    public ResultDTO getTesOrder1(Long orderId) {
        try {
            ResponseDTO<OrderDRO> responseDTO = zsNormalOrderRemoteService.getOrderById(orderId);
            return new ResultDTO<>(true, responseDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/remote/addOrder")
    @ResponseBody
    public ResultDTO remoteAddOrder() {
        NewOrderDIO newOrderDIO = new NewOrderDIO();

        newOrderDIO.setChannelId(GlobalConsts.CHANNEL_WEIXIN_ID);
        newOrderDIO.setType(OrderConsts.ORDER_TYPE_NEW);
        newOrderDIO.setUserName("小A");
        newOrderDIO.setGender(1);
        newOrderDIO.setTelephone("15810520852");
        newOrderDIO.setPrepayAmount(3000);

        // 北京
        newOrderDIO.setCityId(500100);
        newOrderDIO.setStreet("重庆市渝北区金竹苑");
        newOrderDIO.setAddress("测试11");

        newOrderDIO.setLatitude(29.636303);
        newOrderDIO.setLongitude(106.489723);

        // 服务分类 家电维修
        newOrderDIO.setServCategId(1001);


        // 产品信息
        OrderProductDIO orderProductDIO = new OrderProductDIO();
        orderProductDIO.setProductId(10285);
        orderProductDIO.setNumber(2);
        orderProductDIO.setRemark("产品备注");
        List<OrderProductDIO> list = Lists.newArrayList(orderProductDIO);
        newOrderDIO.setOrderProductList(list);

        // 服务项信息
        OrderServiceItemDIO orderServiceItemDIO = new OrderServiceItemDIO();
        orderServiceItemDIO.setServItemId(0);
        orderServiceItemDIO.setServItemName("一口价服务项");
        orderServiceItemDIO.setServItemGroupId(0);
        orderServiceItemDIO.setServItemGroupName("一口价服务项组");
        orderServiceItemDIO.setNumber(1);
        orderServiceItemDIO.setItemPrice(10000);

        OrderServiceItemDIO orderServiceItemDIO1 = new OrderServiceItemDIO();
        orderServiceItemDIO1.setServItemId(0);
        orderServiceItemDIO1.setServItemName("一口价服务项");
        orderServiceItemDIO1.setServItemGroupId(0);
        orderServiceItemDIO1.setServItemGroupName("一口价服务项组");
        orderServiceItemDIO1.setNumber(1);
        orderServiceItemDIO1.setItemPrice(10000);

        newOrderDIO.setOrderServiceItemList(Lists.newArrayList(orderServiceItemDIO, orderServiceItemDIO1));

        newOrderDIO.setRemark("订单备注");
        newOrderDIO.setFinalPrice(2);

        ResponseDTO<NewOrderDRO> newOrderDROResponseDTO = zsNormalOrderRemoteService.addOrder(newOrderDIO);
        return ResultDTO.success(newOrderDROResponseDTO);
    }

    /**
     *
     * 新增店铺工单
     * @return
     */
    @RequestMapping(value = "test/remote/addShopOrder")
    @ResponseBody
    public ResultDTO remoteAddShopOrder(@RequestBody NewShopOrderDIO newShopOrderDIO) {
        ResponseDTO<NewOrderDRO> newOrderDROResponseDTO = shopOrderRemoteService.addOrder(newShopOrderDIO);
        if (newOrderDROResponseDTO.getStatusBool()) {
            return ResultDTO.success(newOrderDROResponseDTO.getData());
        } else {
            return ResultDTO.fail(newOrderDROResponseDTO.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/saveOrder")
    @ResponseBody
    public ResultDTO saveTestOrder() {
        ZsNormalNewOrderDTO order = new ZsNormalNewOrderDTO();
        order.setChannelId(10197);
        order.setType(OrderConsts.ORDER_TYPE_NEW);
        order.setUserName("小A");
        order.setGender(1);
        order.setTelephone("15810520852");

        // 北京
        order.setCityId(340100);
        order.setStreet("站塘路东苑雅居小区9栋1单元202室");
        order.setAddress("测试11");

        order.setLatitude(31.25121121322262);
        order.setLongitude(121.49102172615575);

        // 服务分类 家电维修
        order.setServCategId(1001);
        // 产品信息
        OrderProductDTO orderProductDTO = new OrderProductDTO();
        orderProductDTO.setProductId(10285);
        orderProductDTO.setNumber(1);
        orderProductDTO.setRemark("产品备注");

        List<OrderProductDTO> list = Lists.newArrayList(orderProductDTO);
        order.setOrderProductList(list);

        order.setRemark("订单备注");

        try {
            OrderBO orderBO = zsNormalOrderBService.insertOrder(order);
            elasticSearchDataSyncService.syncWorkIndex(orderBO.getOrder().getOrderId());

            return new ResultDTO<>(true, orderBO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/channel/getOrder")
    @ResponseBody
    @SuppressWarnings("all")
    public ResultDTO getTestChannelOrder() {

        try {
            ResponseDTO<OrderDRO> responseDTO = zsChannelOrderRemoteService.getOrderById(324890509976154112L);
            return new ResultDTO<>(true, responseDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }


    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/shop/orderamount")
    @ResponseBody
    @SuppressWarnings("all")
    public ResultDTO getOrderamount(@RequestBody OrderAmountByCalcDIO orderAmountByCalcDIO) {
        com.zmn.common.dto2.ResponseDTO<OrderAmountByCalcDRO> orderAmountByCalc = shopOrderRemoteService.getOrderAmountByCalc(orderAmountByCalcDIO);
        return new ResultDTO<>(true, orderAmountByCalc);
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/channel/saveOrder")
    @ResponseBody
    @SuppressWarnings("all")
    public ResultDTO saveTestChannelOrder() {

        NewOrderDIO order = new NewOrderDIO();
        order.setChannelId(1530);
        order.setType(OrderConsts.ORDER_TYPE_NEW);
        order.setUserName("目");
        order.setGender(1);
        order.setTelephone("15062303220");

        // 北京
        order.setCityId(500100);
        order.setStreet("长安汽车");
        order.setAddress("12");
//
//        order.setLatitude(29.636303);
//        order.setLongitude(106.489723);
        order.setLatitude(29.506747);
        order.setLongitude(106.579231);

        // 服务分类 家电维修
        order.setServCategId(1002);
        // 产品信息
        OrderProductDIO orderProductDTO = new OrderProductDIO();
        // orderProductDTO.setProductId(10312);
        orderProductDTO.setProductId(10024);
        orderProductDTO.setBrandId(1002);
        orderProductDTO.setNumber(1);
        orderProductDTO.setRemark("允许工程师向用户收费。123");

        List<OrderProductDIO> list = Lists.newArrayList(orderProductDTO);
        order.setOrderProductList(list);

        //order.setDutyTime(DateUtil.parse("2018-12-15 11:30", DateUtil.FORMAT_DATETIME));
        order.setRemark("订单备注");
        order.setOperatorType(8);
        order.setOperatorId(324861852444000256L);
        order.setOperator("15062303220");

        try {
            ResponseDTO<NewOrderDRO> responseDTO = zsChannelOrderRemoteService.addOrder(order);
            return new ResultDTO<>(true, responseDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/channel/updateOrder")
    @ResponseBody
    @SuppressWarnings("all")
    public ResultDTO saveTestUpdateOrder() {

        UpdateOrderDIO order = new UpdateOrderDIO();
        order.setChannelId(1076);
        order.setOrderId(352507322226016256L);
        order.setWorkId(352507322226016256L);
        order.setType(OrderConsts.ORDER_TYPE_NEW);
        order.setUserName("目");
        order.setGender(1);
        order.setTelephone("15062303220");

        // 北京
        order.setCityId(330700);
        order.setStreet("金华市兰溪市兰溪市");
        order.setAddress("12");
//
        order.setLatitude(29.214044);
        order.setLongitude(119.466602);

        // 服务分类 家电维修
        order.setServCategId(1002);
        // 产品信息
        OrderProductDIO orderProductDTO = new OrderProductDIO();
        // orderProductDTO.setProductId(10312);
        orderProductDTO.setProductId(10006);
        orderProductDTO.setBrandId(1002);
        orderProductDTO.setNumber(1);
        orderProductDTO.setRemark("允许工程师向用户收费。123");

        List<OrderProductDIO> list = Lists.newArrayList(orderProductDTO);
        order.setOrderProductList(list);

        //order.setDutyTime(DateUtil.parse("2018-12-15 11:30", DateUtil.FORMAT_DATETIME));
        order.setRemark("订单备注");
        order.setOperatorType(8);
        order.setOperatorId(1L);
        order.setOperator("test");

        try {
            ResponseDTO<NewOrderDRO> responseDTO = zsChannelOrderRemoteService.updateOrder(order);
            return new ResultDTO<>(true, responseDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/syncOrder")
    @ResponseBody
    public ResultDTO syncTestOrder(Long orderId) {
        try {
            elasticSearchDataSyncService.syncWorkIndex(orderId);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false, e.getMessage());
        }
    }

    /**
     * 新增普通单
     *
     * @return
     */
    @RequestMapping(value = "test/master")
    @ResponseBody
    public ResultDTO master(DistributeMasterQuery distributeMasterQuery) {
        return ResultDTO.success();
    }

    /**
     * 新增大客户单
     *
     * @return
     */
    @RequestMapping(value = "test/bcustomer")
    @ResponseBody
    public ResultDTO bcustomer(@RequestBody ZsBCustomerNewOrderDTO zsBCustomerNewOrderDTO) {
        logger.info("{}", zsBCustomerNewOrderDTO);
        return ResultDTO.success();
    }

    /**
     * 新增大客户单
     *
     * @return
     */
    @RequestMapping(value = "test/update/bcustomer")
    @ResponseBody
    public ResultDTO updateBcustomer(@RequestBody ZsBCustomerUpdateOrderDTO zsBCustomerUpdateOrderDTO) {
        logger.info("{}", zsBCustomerUpdateOrderDTO);
        return ResultDTO.success();
    }

    /**
     * 查询大客户单
     *
     * @return
     */
    @RequestMapping(value = "test/getOrderById")
    @ResponseBody
    public ResultDTO getOrderById(Long orderId) {
        ZsNormalUpdateOrderVO zsNormalUpdateOrderVO = zsOrderWorkBService.findUpdateOrderWorkVO(orderId, orderId);
        return ResultDTO.success(zsNormalUpdateOrderVO);
    }

    /**
     * 查询大客户单
     *
     * @return
     */
    @RequestMapping(value = "test/insOrder")
    @ResponseBody
    public ResultDTO getInsOrder(Long orderId) {
        InsOrder insOrder = insOrderService.findInsOrderByOrderId(orderId);
        return ResultDTO.success(insOrder);
    }

    /**
     * 查询大客户单
     *
     * @return
     */
    @RequestMapping(value = "test/cancelOrder")
    @ResponseBody
    public ResultDTO cancelOrder(Long orderId) {
        ZsCancelDTO zsCancelDTO = new ZsCancelDTO();
        zsCancelDTO.setOrderId(orderId);
        zsCancelDTO.setWorkId(orderId);

        // 操作人信息
        zsCancelDTO.setOperator("test");
        zsCancelDTO.setOperatorId(1L);
        zsCancelDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_BCUSTOMER);
        try {
            zsNormalWorkBService.saveCancel(zsCancelDTO);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "test/complete")
    @ResponseBody
    public ResultDTO complete() {
        try {
            CompleteDTO completeDTO = new CompleteDTO();
            completeDTO.setOrderId(307229685639950336L);
            completeDTO.setWorkId(307229685639950336L);
            completeDTO.setAgent(1);
            completeDTO.setMasterId(131986);
            completeDTO.setOperatorType(1);
            zsNormalMasterWorkBService.completeOrder(completeDTO);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "test/syncOuterOrder")
    @ResponseBody
    public ResultDTO syncOuterOrder() {
        try {
            CompleteDTO completeDTO = new CompleteDTO();
            completeDTO.setOrderId(307229685639950336L);
            completeDTO.setWorkId(307229685639950336L);
            completeDTO.setAgent(1);
            completeDTO.setMasterId(131986);
            completeDTO.setOperatorType(1);
            zsNormalMasterWorkBService.completeOrder(completeDTO);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

}
